from posixpath import dirname
import qrcode
from PIL import Image
import pathlib
import os
import sys
from Match_Template import *
from list import *
savepath="D:\\markpic\\1203\\04\\kc\\kc_181106\\pc_1\\scan"
root=pathlib.Path(os.path.dirname(os.path.realpath(sys.argv[0])))
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=8,
    border=4,
)
#img1.show()

#img.get_image().show()
#img.save(root/'qrcode.png')
#img=Image.open(root/'qrcode.png')

def ROTATE_180(file1,file2):
    global root
    img = Image.open(file1)
    img1 = img.rotate(180)  # 引用固定的常量值
    img1.save(root/"temp1.jpg")
    img = Image.open(file2)
    img1 = img.rotate(180)   # 引用固定的常量值
    img1.save(root/"temp2.jpg")
    img1=cv2.imread(str(root/'temp1.jpg')) 
    return img1
def switchImgROTATE_180(file1,file2):
    global root
    img = Image.open(file1)
    img1 = img.rotate(180)  # 引用固定的常量值
    img1.save(root/"temp2.jpg")
    img = Image.open(file2)
    img1 = img.rotate(180)  # 引用固定的常量值
    img1.save(root/"temp1.jpg")
    img1=cv2.imread(str(root/'temp1.jpg')) 
    return img1
def switchImg(file1,file2):
    global root
    img = Image.open(file1)
    img.save(root/"temp2.jpg")
    img = Image.open(file2)
    img.save(root/"temp1.jpg")
    img1=cv2.imread(str(root/'temp1.jpg')) 
    return img1
def savepic(pathImg1,pathImg2,id,page):
    global sfbl
    global s
    qr.clear()
    qr.add_data(str(id))
    qr.make(fit=True)
    img1= Image.open(pathImg1)
    img2= Image.open(pathImg2)
    qrimg = qr.make_image(fill_color="black", back_color="white")
    layer= Image.new('RGBA',img1.size,(255,255,255,0))
    layer.paste(qrimg,(800,200))
    img_after=Image.composite(layer,img1,layer)
    
    #layer.paste(Image.new('RGBA',(800,300),(255,255,255,255)),(1500,1250))
    #img_after=Image.composite(layer,img,layer)
    
    img_after = img_after.resize((int(img_after.size[0]*sfbl),int(img2.size[1]*sfbl)))
    img_after.save(f"{savepath}/1{page+1:05d}_{s_}{page+1:03d}_{s}.jpg")
    img_after = img2.resize((int(img2.size[0]*sfbl),int(img2.size[1]*sfbl)))
    img_after.save(f"{savepath}/1{page+2:05d}_{s_}{page+2:03d}_{s}.jpg")

template = cv2.imread(str(root/'t.jpg')) #页码标记，判断正反用
template = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)


if __name__ =="__main__":
    sfbl=1 #缩放比例
    i=1
    s_="101"
    s="41646675"
    n=0
    for dir_path, subpaths, files in os.walk(root/"img", True):
        print(len(files))
        for i in range(0,len(files),2):
            id=idlist[n]
            n=n+1
            print(id)
            img = Image.open(str(root/f"img/{files[i]}"))
            pathImg1=str(root/f"img/{files[i]}")
            pathImg2=str(root/f"img/{files[i+1]}")
            img1=cv2.imread(pathImg1)
            img2=cv2.imread(pathImg2)
            if(MT(img1,template)):
                savepic(pathImg1,pathImg2,id,i)
                continue
            img1=ROTATE_180(pathImg1,pathImg2)
            if(MT(img1,template)):
                savepic(str(root/"temp1.jpg"),str(root/"temp2.jpg"),id,i)
                continue
            img1=switchImg(pathImg1,pathImg2)
            if(MT(img1,template)):
                savepic(str(root/"temp1.jpg"),str(root/"temp2.jpg"),id,i)
                continue
            img1=switchImgROTATE_180(pathImg1,pathImg2)
            if(MT(img1,template)):
                savepic(str(root/"temp1.jpg"),str(root/"temp2.jpg"),id,i)
                continue
            print("error")
            print(pathImg1)
            print(pathImg2)

            cv2.imshow('error',resize_image(img1,900))
            cv2.waitKey(0)
            cv2.destroyAllWindows()
            
    os.remove(str(root/"temp1.jpg"))
    os.remove(str(root/"temp2.jpg"))

